import java.awt.*;

public class Mob extends Rectangle {
	public int xC, yC;
	public int mobSize = Room.blockSize;
	public int mobID = Value.mobAir; 
	public boolean inGame = false; 
	public int mobWalk = 0;	
	public int upward = 0, downward = 1, right = 2, left = 3;
	public int direction = right;
	public boolean hasUpward = false;
	public boolean hasDownward = false;
	public boolean hasLeft = false;
	public boolean hasRight = false;
	public Mob() {
		
		}
	

	public void spawnMob(int mobID) {
		for (int y=0;y<Screen.room.block.length;y++) {
			if(Screen.room.block[y][0].groundId == Value.groundGrass) {
				setBounds(Screen.room.block[y][0].x, 
						Screen.room.block[y][0].y,
						mobSize + 10,
						mobSize + 10);
				xC = 0;
				yC = y;
			}
		}
		
		//setBounds(10,10,100,100);
		
		this.mobID = mobID;
		
		inGame = true;
		
		//System.out.println("Spwn");
	}
	
	public void deleteMob() {
		inGame = false;
	}
	
	public void looseHealth() {
		Screen.health -= 1;
	}
	
	public int walkFrame = 0, walkSpeed = 40;
	public void physic() {
		if(walkFrame >= walkSpeed) {
			if(direction == right) {
				x += 1;
			} else if (direction == upward) {
				y -= 1;				
			} else if (direction == downward){
				y += 1;
			} else if (direction == left) {
				x -= 1;
			}
			
			mobWalk += 1;
			
			if(mobWalk == Screen.room.blockSize + 10) {
				if(direction == right) {
					xC += 1;
					hasRight = true;
				} else if (direction == upward) {
					yC -= 1;
					hasUpward = true;
				} else if (direction == downward){
					yC += 1;
					hasDownward = true;
				} else if (direction == left) {
					xC -= 1;
					hasLeft = true;
				} 
				
				if(!hasUpward){
					try{
						if(Screen.room.block[yC+1][xC].groundId == Value.groundGold){
							direction = downward;						
						}
					} catch (Exception e) {}
				}
				if(!hasDownward){
					try{
						if(Screen.room.block[yC-1][xC].groundId == Value.groundGold){
							direction = upward;
							
						}
					} catch (Exception e) {}
				}
				
				if(!hasLeft){
					try{
						if(Screen.room.block[yC][xC+1].groundId == Value.groundGold){
							direction = right;
							
						}
					} catch (Exception e) {}
				}
				
				if(!hasRight){
					try{
						if(Screen.room.block[yC][xC-1].groundId == Value.groundGold){
							direction = left;
							
						}
					} catch (Exception e) {}
				}
				
				if(Screen.room.block[yC][xC].airId == Value.finishLine){
					deleteMob();
					looseHealth();
				}
				
				mobWalk = 0;
				hasUpward = false;
				hasDownward = false;
				hasLeft = false;
				hasRight = false;
			}
			
			walkFrame = 0;
		} else {
			walkFrame += 1;
		}
	}
		
	public void draw(Graphics g) {
		if(inGame) {
			g.drawImage(Screen.tileset_ground[mobID],x,y,width,height,null);
		}
		
	}

}
